package com.platform.omp.OperationalModule.customerInjure.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.platform.omp.OperationalModule.customerInjure.dao.DCustomerinjureDao;
import com.platform.omp.OperationalModule.customerInjure.dao.HrOrgDeptDao;
import com.platform.omp.OperationalModule.customerInjure.entity.DCustomerinjure;
import com.platform.omp.OperationalModule.customerInjure.entity.DCustomerinjureQuery;
import com.platform.omp.OperationalModule.customerInjure.entity.HrOrgDept;
import com.platform.omp.OperationalModule.customerInjure.service.CustomerInjureService;
import com.platform.omp.common.exceptionhandler.MyException;
import com.platform.omp.common.utils.Result;
import com.platform.omp.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 类描述：
 *
 * @ClassName CustomerInjureServiceImpl
 * @Author liucl
 * @Date 2022/3/15
 */
@Service
public class CustomerInjureServiceImpl implements CustomerInjureService {

    @Autowired
    private DCustomerinjureDao dCustomerinjureDao;

    /**
     * 新增客伤管理
     *
     * @param dCustomerinjure
     * @return
     */
    @Override
    public int insertInjure(DCustomerinjure dCustomerinjure) {
        return dCustomerinjureDao.insert(dCustomerinjure);
    }

    /**
     * 修改客伤管理
     *
     * @param dCustomerinjure
     * @return
     */
    @Override
    public int updateInjure(DCustomerinjure dCustomerinjure) {
        return dCustomerinjureDao.updateByPrimaryKey(dCustomerinjure);
    }

    /**
     * 删除客伤管理
     *
     * @param ids 主键id
     * @return
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void deleteInsertInjure(String ids) throws MyException {
        String[] dictIds = Convert.toStrArray(ids);
        for (String id : dictIds) {
            dCustomerinjureDao.deleteByPrimaryKey(id);
        }
    }

    @Autowired
    private HrOrgDeptDao deptDao;

    @Override
    public List<HrOrgDept> selectHrOrgDeptList() {
        return deptDao.selectHrOrgDeptList();
    }

    /**
     * 分页条件查询客伤列表
     *
     * @param offset
     * @param limit
     * @param injure
     * @return
     */
    @Override
    public Result queryInjurePage(Integer offset, Integer limit, DCustomerinjureQuery injure) {
        Page page = PageHelper.offsetPage(offset, limit);
        DCustomerinjure dCustomerinjure = new DCustomerinjure();
        BeanUtil.copyProperties(injure,dCustomerinjure);
        List<DCustomerinjure> injureList = dCustomerinjureDao.queryInjurePage(dCustomerinjure);
        return Result.ok().count(page.getTotal()).data(injureList).code(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询客伤信息
     *
     * @param id
     * @return
     */
    @Override
    public Result queryCustomerInjureId(long id) {
        DCustomerinjure result = dCustomerinjureDao.selectByPrimaryKey(String.valueOf(id));
        return Result.success(result);
    }
}
